package com.yunshipei.wps;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.IBinder;
import android.widget.Toast;

import java.io.File;

import cn.wps.moffice.service.OfficeService;

public class WPSManager {

    private WPSManager() {
    }

    private static final class WPSManagerHolder {
        private static final WPSManager instance = new WPSManager();
    }

    public static WPSManager getInstance() {
        return WPSManagerHolder.instance;
    }

    private SettingPreference settingPreference;

    private boolean flag = false;

    public WPSManager init(Context mContext) {
        if (!flag) {
            flag = true;
            settingPreference = new SettingPreference(mContext);
            settingPreference.setSettingParam(Define.KEY, mContext.getPackageName());
            mContext.startService(new Intent(mContext, MOfficeClientService.class));
            final Intent intent = new Intent(Define.PRO_OFFICE_SERVICE_ACTION);
            intent.setPackage("com.kingsoft.moffice_pro");
            intent.putExtra("DisplayView", true);
            boolean isbindService = false;
            try {
                isbindService = mContext.bindService(intent, mConnection, Service.BIND_AUTO_CREATE);
            } catch (SecurityException e) {
                throw e;
            }
            if (!isbindService) {
                // bind failed, maybe wps office is not installed yet.
                mContext.unbindService(mConnection);
            }
        }
        return this;
    }

    private OfficeService mService;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = OfficeService.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };

    public void unBindWPS(Context context) {
        if (mService != null) {
            context.unbindService(mConnection);
        }
    }

    public void openFile(String fileURL, Context mContext) {
        File file = new File(fileURL);
        if (Util.IsWPSFile(file)) {
            OpenFile openFile = new OpenFile(mService, mContext);
            //如果是WPS支持打开的文件，则打开
            if (Define.WPS_OPEN_AIDL.equals(settingPreference.getSettingParam(Define.WPS_OPEN_MODE, ""))) {
                openFile.openDocument(fileURL);
            } else {
                boolean isopened = openFile.openOtherFile(fileURL);
                if (!isopened) {
                    Toast.makeText(mContext, "第三方打开失败", Toast.LENGTH_LONG).show();
                }
            }
        } else {
            //不支持打开的文档则用户选择打开方式
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            String type = Util.getMIMEType(file);
            intent.setDataAndType(Uri.fromFile(file), type);
            mContext.startActivity(intent);
        }
    }
}
